package com.example.demo.config;

import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

/**
 * @author
 * @create 86153-2021
 */
@EnableWebSecurity
public class security extends WebSecurityConfigurerAdapter {

    //链式编程
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //首页所有人可以访问，其他的需要权限
        //请求授权的规则
        http.authorizeRequests()
                .antMatchers("/index").permitAll()
                .antMatchers("/1").hasRole("vip1")
                .antMatchers("/2").hasRole("vip2")
                .antMatchers("/3").hasRole("vip3")
                .antMatchers("/first").hasRole("vip3");

        //没有权限到等路页面
            //login
        http.formLogin();

        http.csrf().disable();
        http.logout().logoutSuccessUrl("/");
        http.rememberMe();
    }

    //认证,从缓存中得到数据
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {


        auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
                .withUser("shekai").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1","vip2","vip3")
                .and()
                .withUser("1").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1")
                .and()
                .withUser("2").password(new BCryptPasswordEncoder().encode("123456")).roles("vip2")
                .and()
                .withUser("3").password(new BCryptPasswordEncoder().encode("123456")).roles("vip3");

    }

    //认证，从数据库中得到数据

}
